home *** CD-ROM | disk | FTP | other *** search
- Path: rcp6.elan.af.mil!rscernix!danpop
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c
- Subject: Re: microseconds??
- Date: 5 Mar 96 12:03:36 GMT
- Organization: CERN European Lab for Particle Physics
- Message-ID: <danpop.826027416@rscernix>
- References: <4h62s9$2o7@chaos.dac.neu.edu> <4h7netINN1hk@anvil.ugrad.cs.ubc.ca>
- NNTP-Posting-Host: ues5.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
-
- In <4h7netINN1hk@anvil.ugrad.cs.ubc.ca> c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
-
- >In article <4h62s9$2o7@chaos.dac.neu.edu>,
- >hadi abedi <habedi@lynx.dac.neu.edu> wrote:
- >>Hello,
- >> Thanks for taking your time to read this.
- >>
- >> Is there a way to compute microsecond intervals
- >> with C?
- >> I'd like to time a sorting routine on different
- >> list sizes. I am aware of the 'time' function on
- >> the UNIX system, but I'd like to know how to do
- >> it in C.
- >> Thanks.
- >
- >The _best_ way to do this under a particular environment is up to, well, that
- >particular environment. The standard C way of timing is the the clock()
- >function. Converting to seconds means dividing by the symbolic constant
- >CLOCKS_PER_SEC defined in <time.h>. Hopefully, this is a few order of
- >magnitudes greater than 1.
- >
- >On this HP-UX system, the actual value is:
- >
- >/* ANSI C time constants, types, and structures */
- >
- >#ifdef _INCLUDE__STDC__
- ># define CLOCKS_PER_SEC 1000000
- >
- ># ifndef NULL
- ># define NULL 0
- ># endif
- >
- >So you see, the clock() function can have microsecond resolution (I'm not
- >saying that the hardware does, just the clock() function).
-
- The sad truth is that clock() cannot have a better resolution than the
- hardware it runs on.
-
- >It's likely that it is implemented with the best possible method that yields
- >the greatest resolution. The above clearly shows that CLOCKS_PER_SEC does not
- >have to be restricted to the preemptive clock frequency of the operating system
- >(i.e. the HZ value).
-
- The resolution of clock() is however restricted to the CLK_TCK value
- (i.e. the value produced by sysconf(_SC_CLK_TCK)). The value of
- CLOCKS_PER_SEC is completely irrelevant to the resolution provided by
- clock(), even if you have to use it to convert to seconds. Proof:
-
- hpplus06:~/tmp 8> uname -a
- HP-UX hpplus06 A.09.05 A 9000/735 2010153393 two-user license
- hpplus06:~/tmp 9> cat clock.c
- #include <stdio.h>
- #include <time.h>
-
- int main(int argc, char *argv[])
- {
- clock_t s,f;
-
- s = clock();
- while ((f = clock()) == s) ;
- printf("%ld %f\n", (long)CLOCKS_PER_SEC, (double)(f-s) / CLOCKS_PER_SEC);
- return 0;
- }
- hpplus06:~/tmp 10> cc clock.c
- hpplus06:~/tmp 11> ./a.out
- 1000000 0.010000
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-